﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Shell;
using Microsoft.Win32;
using System.Diagnostics;
using Microsoft.WindowsAPICodePack.Dialogs;

namespace Snip
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private static int[] intervalValues = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 30, 60 };
        private SplitOptions options;

        public MainWindow()
        {
            options = App.SplitOptions;

            InitializeComponent();
            DataContext = options;
            DetailsBorder.Visibility = Visibility.Collapsed;
            SourceFile = Snip.Properties.Settings.Default.RecentFile;

            List<TimeSpan> intervalOptions = new List<TimeSpan>();
            for (int i = 0; i < intervalValues.Length; i++)
            {
                intervalOptions.Add(TimeSpan.FromMinutes(intervalValues[i]));

            }
            IntervalComboBox.ItemsSource = intervalOptions;

            if (App.UseCommonDialogForFolderBrowse)
            {
                BrowseFolderButton.Click += new RoutedEventHandler(BrowseFolderButton_Click);
            }
            else
            {
                BrowseFolderButton.Click += new RoutedEventHandler(BrowseFolderButton_Click2);
            }

        }

        public string SourceFile
        {
            get { return (string)GetValue(SourceFileProperty); }
            set { SetValue(SourceFileProperty, value); }
        }

        public static readonly DependencyProperty SourceFileProperty =
            DependencyProperty.Register("SourceFile", typeof(string), typeof(MainWindow), new UIPropertyMetadata(string.Empty));

        private void SplitButton_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(SourceFile))
            {
                MessageBox.Show(this, "Specified source file path is empty. What do you expect?", "Source path is empty", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            else if (!File.Exists(SourceFile))
            {
                MessageBox.Show(this, "No such file exists! Check the source file field and try again.", "No such file exists", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            SplitButton.IsEnabled = false;

            string fileName = SourceFile;
            Snip.Properties.Settings.Default.RecentFile = fileName;

            ProgressDialog dialog = new ProgressDialog(fileName, options);
            dialog.Owner = this;
            bool? result = dialog.ShowDialog();

            SplitButton.IsEnabled = true;
        }

        private void BrowseFileButton_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.CheckFileExists = true;
            dialog.CheckPathExists = true;
            dialog.DefaultExt = ".txt";
            dialog.Filter = "Text Files|*.txt|All Files|*.*";

            if (dialog.ShowDialog(this).Value)
            {
                SourceFile = dialog.FileName;
            }
        }

        private void BrowseFolderButton_Click(object sender, RoutedEventArgs e)
        {
            CommonOpenFileDialog cfd = new CommonOpenFileDialog();
            cfd.IsFolderPicker = true;
            if (cfd.ShowDialog() == CommonFileDialogResult.Ok)
            {
                options.DestFolder = cfd.FileName;
            }
        }

        private void BrowseFolderButton_Click2(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
            dialog.Description = "Select a folder to save splitted files into.";
            if (!string.IsNullOrWhiteSpace(options.DestFolder))
            {
                dialog.SelectedPath = options.DestFolder;
            }

            if (dialog.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                options.DestFolder = dialog.SelectedPath;
            }
        }

        private void OptionsExpanderDummy_Expanded(object sender, RoutedEventArgs e)
        {
            DetailsBorder.Visibility = Visibility.Visible;
        }

        private void OptionsExpanderDummy_Collapsed(object sender, RoutedEventArgs e)
        {
            DetailsBorder.Visibility = Visibility.Collapsed;
        }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void Hyperlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
        {
            Process.Start("http://snip.codeplex.com");
            e.Handled = true;
        }
    }
}
